//
//  SMRTSyncTimePickerViewController.m
//  SMRTv1
//
//  Created by Ding on 8/13/13.
//  Copyright (c) 2013 Trans-Intelligence. All rights reserved.
//

#import "SMRTSyncTimePickerViewController.h"

@interface SMRTSyncTimePickerViewController ()

@end

@implementation SMRTSyncTimePickerViewController
@synthesize timePicker=_timePicker;
@synthesize agency=_agency;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
       
        
    }
    return self;
}
- (id)initWithAgency:(Agency *)agency nibNameOrNil:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.agency=agency;
        
    }
    return self;
}

- (void) dealloc
{
    [_timePicker release];
    [_agency release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.view.opaque = NO;
    
    if (!_timePicker) {
        _timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, 325, 350)];
        _timePicker.datePickerMode = UIDatePickerModeTime;
//        _timePicker.hidden = YES;
        NSDateFormatter *inputFormat = [[[NSDateFormatter alloc] init] autorelease];
        [inputFormat setDateFormat:@"yyyy-MM-dd hh:mm aa"];
        NSDate *inputDate = [inputFormat dateFromString: @"2012-01-01 01:00 AM"];
        if (![self.agency.syncTime isEqualToString:@""]) {
            NSLog(@"2012-01-01 %@",self.agency.syncTime);
            inputDate = [inputFormat dateFromString: [NSString stringWithFormat:@"2012-01-01 %@",self.agency.syncTime]];
        }
        [_timePicker setDate:inputDate];
        [self.view addSubview:_timePicker];
    }
    [[self navigationController] setNavigationBarHidden:NO];
    UIBarButtonItem* item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(selectTime:)] autorelease];
    self.navigationItem.rightBarButtonItem = item;
    self.title=@"Select sync time";
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) selectTime:(id) sender
{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.timeStyle=NSDateFormatterShortStyle;
    [self.agency setSyncTime:[df stringFromDate:self.timePicker.date]];
    [XMLParser updateAgency:self.agency];
    [df release];
//    [self updateViewController];

    [self.timePicker setHidden:YES];
    self.navigationItem.rightBarButtonItem=nil;
    [self.navigationController popViewControllerAnimated:YES];
    
    
}


@end
